Conversation
There was a problem hiding this comment.
Actionable comments posted: 5
🧹 Nitpick comments (1)
035-search-insert-position/memo.md (1)
74-74: Add language specifier to code fence for better readability.The code block would benefit from specifying
pythonas the language for syntax highlighting.Apply this change:
-``` +```python class Solution:
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (4)
035-search-insert-position/memo.md(1 hunks)035-search-insert-position/step1.py(1 hunks)035-search-insert-position/step2.py(1 hunks)035-search-insert-position/step3.py(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
035-search-insert-position/step2.py (2)
035-search-insert-position/step1.py (2)
Solution(3-6)searchInsert(4-6)035-search-insert-position/step3.py (2)
Solution(1-15)searchInsert(2-15)
035-search-insert-position/step1.py (2)
035-search-insert-position/step2.py (2)
Solution(2-17)searchInsert(3-17)035-search-insert-position/step3.py (2)
Solution(1-15)searchInsert(2-15)
🪛 LanguageTool
035-search-insert-position/memo.md
[grammar] ~32-~32: Ensure spelling is correct
Context: ...数字がTargetよりも大きければそのまま右側を走査、 小さければ左側を走査する そのまま見つかればIndexをreturn 見つからない場合、 ## Step2での気づき 解答解説を見た時、left, ...
(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)
🪛 markdownlint-cli2 (0.18.1)
035-search-insert-position/memo.md
74-74: Fenced code blocks should have a language specified
(MD040, fenced-code-language)
🪛 Ruff (0.14.6)
035-search-insert-position/step2.py
3-3: Undefined name List
(F821)
035-search-insert-position/step3.py
2-2: Undefined name List
(F821)
035-search-insert-position/step1.py
6-7: Expected an indented block after if statement
(invalid-syntax)
🔇 Additional comments (4)
035-search-insert-position/step3.py (2)
3-8: Binary search initialization looks good.The pointer initialization and loop condition (
left <= right) are correct for this binary search implementation.
11-15: Pointer updates and insertion logic are correct.The adjustments
right = mid - 1andleft = mid + 1properly narrow the search space, and returningleftas the insertion position is correct.035-search-insert-position/step1.py (1)
1-4: Import and class definition are correct.Good use of the
typing.Listimport for type hints.035-search-insert-position/step2.py (1)
4-6: Pointer initialization is correct.The setup with
left = 0andright = len(nums) - 1is appropriate for binary search.
| 見つからない場合のreturnするIndexの考え方も吸収できた。 | ||
|
|
||
| ### 間違えていたところとしては、 | ||
| 1. left <= rightとしていなかった。 |
There was a problem hiding this comment.
念のため質問させてください。なぜ left <= right としないと意図しない挙動になるのか説明してみていただけますか?また、
- left より左側の要素にはどのような特徴があるか?
- left の位置の要素にはどのような特徴があるか?
- left < x < right の x の位置の要素にはどのような特徴があるか?
- right の位置の要素にはどのような特徴があるか?
- right より右側の要素にはどのような特徴があるか?
それぞれ答えてみていただけますか?
| 1. left <= rightとしていなかった。 | ||
| 2. right, leftの値更新の際の1ずらすところができていなかった。 | ||
|
|
||
| right = mid - 1, left = mid + 1としないと、left < rightという制約を超えることがないため無限ループを引き起こしてしまう。 |
There was a problem hiding this comment.
こちらについても念のため質問させてください。
- なぜ right = mid となっているところ right = mid - 1 としても正しい答えが返って来ますか?
- 同様に left = mid となっているところを left = mid + 1 としても正しい答えが返って来ますか?
- right や left を動かし過ぎて、答えとなるインデックスを通り過ぎてしまうことはありませんか?
|
|
||
| right = mid - 1, left = mid + 1としないと、left < rightという制約を超えることがないため無限ループを引き起こしてしまう。 | ||
|
|
||
| ## GPTからもらった解答 |
There was a problem hiding this comment.
https://nuc.hatenadiary.org/entry/2025/11/29/ の「二分探索を読めるか」の項も参考にされることをおすすめします。
There was a problem hiding this comment.
こちら読ませていただきました。大変面白い内容なので他の項も含めて今後参考にさせていただきます。
初PRです。これからよろしくお願いいたします。
leetcodeチャンネルでBinary Searchから取り組むと良いとの投稿を見たので、この問題を選んでみました。
以前までNeetcodeをやっていて、アルゴリズムに関する知見はあるものの、問題はあまり解けず苦戦している状況です。
問題解決力を鍛えるアルゴリズムとデータ構造という本も以前に読みました。
問題リンク
https://leetcode.com/problems/search-insert-position/description/
問題文の概要
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You must write an algorithm with O(log n) runtime complexity.
次に解く予定の問題
153 find-minimum-in-rotated-sorted-array
https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/